package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.common.ServerResponse;
import com.situ.mall.entity.Cart;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;

@Controller
@RequestMapping(value="/cart")
public class CartController {
	@Autowired
	private ICartService cartService;

	@RequestMapping(value="/insert1.shtml")
	public String insert1(Integer productId, Integer num, HttpSession session) {
		cartService.insert(productId, num, session);
		//重定向到/cart/getCartListPage.shtml
		return "redirect:/cart/getCartListPage.shtml";
	}
	
	@RequestMapping(value="/insert.shtml")
	@ResponseBody
	public ServerResponse insert(Integer productId, Integer num, HttpSession session) {
		return cartService.insert(productId, num, session);
	}
	
	@RequestMapping(value="/getCartListPage.shtml")
	public String getCartListPage(HttpSession session, Model model) {
		User user = (User) session.getAttribute("user");
		List<Cart> list = cartService.selectByUserId(user.getId());
		model.addAttribute("list", list);
		System.out.println(list);
		return "cart_list";
	}
	
	@RequestMapping(value="/deleteById.shtml")
	@ResponseBody
	public ServerResponse deleteById(Integer cartId) {
		return cartService.deleteById(cartId);
	}
	
	@RequestMapping(value="/updateQuantityByUserIdAndProductId")
	@ResponseBody
	public ServerResponse updateQuantityByUserIdAndProductId(Integer productId, Integer num, HttpSession session) {
		return cartService.updateQuantityByUserIdAndProductId(productId, num, session);
	}
	
	@RequestMapping(value="/updateCheckStatusByUserIdAndProductId")
	@ResponseBody
	public ServerResponse updateCheckStatusByUserIdAndProductId(Integer productId, Integer checked, HttpSession session) {
		return cartService.updateCheckStatusByUserIdAndProductId(productId, checked, session);
	}
	
}
